/**
 * The Tester class is designed to test the functionality
 * of all the Elderhealth classes via unit tests.
 * 
 * @author Jeremiah Weeden, Aditya Kakrania, Vy Bui
 * @version 5/19/2007
 */

import java.sql.*;
import com.elderhealth.client.Client;
import com.elderhealth.worker.HomecareWorker;
import com.elderhealth.schedule.Schedule;
import com.elderhealth.schedule.Timeslot;
import com.elderhealth.schedule.Appointment;
import com.elderhealth.util.DBManager;

public class Tester
{
   /* instance variables */
    private static DBManager db = DBManager.getInstance();

    /**
     * main is used to run all unit tests
     * 
     * @param  string[]args
     */
    @SuppressWarnings("deprecation")
    public static void main(String[] args)
    {
        try
        {
	        System.out.println("+Start test:");

            // Test DBManager Code
            testDBManager();

            // Test Client Code
            testClient();

            // Test Appointment Code
            testAppointment();

            // Test HomecareWorker Code
            testHomecareWorker();

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }    
    }
    
    /**
     * testClient tests the functionality of the Client class
     *
     * Author: Jeremiah Weeden 
     */
    private static void testClient()
    {
      /* Test getClientsByMatch() */
      Client [] clients = Client.getClientsByMatch("homer", "simPSon");
      if (clients == null)
      {
        System.out.println("\t\t-Error getting getClientsByMatch(\"homer\", \"simPSon\")");
        wrap();
        return;
      }
      System.out.println("\t\t+Retrieved " + clients.length + " clients for Client getClientsByMatch(\"homer\", \"simPSon\")");
      for (int z=0; z < clients.length; z++)
      {
        Client temp = clients[z];
        System.out.println("\t\t\t-" + temp.getClientId() + ":" + temp.getClientName());
      }
    }

    /**
     * testAppointment tests the functionality of the Appointment class
     *
     * Author: Jeremiah Weeden 
     */
    private static void testAppointment()
    {
      /* Test createAppointment */
      Timestamp start = Timestamp.valueOf("2007-05-07 07:00:00");
      Timestamp end = Timestamp.valueOf("2007-05-07 12:00:00");
      Appointment appointment = Appointment.createAppointment(1, 1, start, end);
      
      if (appointment != null)
        System.out.println("\t\t-Successfully created new appointment (with Appointment class) with ID=" + appointment.getAppointmentID());
      else
        System.out.println("\t\t-Error creating new appointment  (with Appointment class) :(");
            
      /* Test getting client from appointment */
      Client apptdClient = appointment.getClient();
      if (apptdClient != null)
        System.out.println("\t\t-Successfully retrieved appointed client. Client is: " + apptdClient.getClientName());
      else
        System.out.println("\t\t-Error retrieving client using appointment class");

      /* Test getting worker from appointment */
      HomecareWorker apptdWorker = appointment.getWorker();    
      if (apptdWorker != null)
        System.out.println("\t\t-Successfully retrieved appointed worker. Worker is: " + apptdWorker.getFirstName() + " " + apptdWorker.getLastName());
      else
        System.out.println("\t\t-Error retrieving client using appointment class");
            
      /* Test deleteAppointment */
      if (appointment.deleteAppointment() == true)
        System.out.println("\t\t-Successfully deleted the new appointment  (with Appointment class) with ID=" + appointment.getAppointmentID());
      else
        System.out.println("\t\t-Error deleting appointment (with Appointment class) with ID=" + appointment.getAppointmentID());    
    }

    
  /**
     * testDBManager  tests the functionality of the DBManager class
     *
     * Author: Aditya Kakrania
     */
    private static void testDBManager()
    {
        /* Test getClients */
        Client[] clients = db.getClients();
        if (clients == null)
        {
            System.out.println("\t\t-Error getting clients");
            wrap();
            return;
        }
        else
        {
            System.out.println("\t\t+Retrieved " + clients.length + " clients");
            for (int z=0; z < clients.length; z++)
            {
                Client temp = clients[z];
                System.out.println("\t\t\t-" + temp.getClientId() + ":" + temp.getClientName());
            }
        }        
        
        /* Test getClientById()  */
        Client tempClient = db.getClientById(1);
        if (tempClient != null)
        {
            System.out.println("\t\t+Found client with Id=1");
            System.out.println("\t\t\t-" + tempClient.getClientName());
        }
        else
        {
            System.out.println("\t\t-Error finding client with Id=1");
            wrap();
            return;
        }
        
        /* Test getClientByName() */
        String fName = "hom";
        String lName = "sim";
        Client [] clients2 = db.getClientsByName(fName, lName);
        if (clients2 == null)
        {
            System.out.println("\t\t-Error getting getClientsByName(" + fName + "," + lName + ")");
            wrap();
            return;
        }
        else
        {
            System.out.println("\t\t+Retrieved " + clients2.length + " clients for getClientsByName(" + fName + "," + lName + ")");
            for (int z=0; z < clients2.length; z++)
            {
                Client temp = clients2[z];
                System.out.println("\t\t\t-" + temp.getClientId() + ":" + temp.getClientName());
            }
        }
        
        /* Test getWorkers() */
        HomecareWorker[] workers = db.getWorkers();
        if (workers == null)
        {
            System.out.println("\t\t-Error getting workers");
            wrap();
            return;
        }
        else
        {
            System.out.println("\t\t+Retrieved " + workers.length + " workers");
            for (int z=0; z < workers.length; z++)
            {
                HomecareWorker temp = workers[z];
                System.out.println("\t\t\t-" + temp.getWorkerId() + ":" + temp.getWorkerName());
            }
        }
        
        /* Test getHomecareWorkerById() */
        HomecareWorker tempHomecareWorker = db.getWorkerById(1);
        if (tempHomecareWorker != null)
        {
            System.out.println("\t\t+Found worker with Id=1");
            System.out.println("\t\t\t-" + tempHomecareWorker.getWorkerName());
        }
        else
        {
            System.out.println("\t\t-Error finding worker with Id=1");
            wrap();
            return;
        }        
        
        /* Test getHomecareWorkerByName() */
        HomecareWorker [] workers2 = db.getWorkersByName("carl", "carlson");
        if (workers2 == null)
        {
            System.out.println("\t\t-Error getting getHomecareWorkersByName(\"carl\", \"carlson\")");
            wrap();
            return;
        }
        System.out.println("\t\t+Retrieved " + workers2.length + " workers for getHomecareWorkersByName(\"carl\", \"carlson\")");
        for (int z=0; z < workers2.length; z++)
        {
            HomecareWorker temp = workers2[z];
            System.out.println("\t\t\t-" + temp.getWorkerId() + ":" + temp.getWorkerName());
        }

        /* Test getWorkerSchedule() */
        if (null == db.getWorkerSchedule(1, null, null))
        {
            System.out.println("\t\t-Error getting worker schedule with Id=1");
            /* Don't quit after this as other tests may still succeed */
        }
        
        /* Test setAppointment() */
        Timestamp start = Timestamp.valueOf("2007-05-07 07:00:00");
        Timestamp end = Timestamp.valueOf("2007-06-07 07:00:00");
        Appointment appointment = db.setAppointment(1, 1, start, end);
        if (appointment != null)
            System.out.println("\t\t-Successfully created new appointment with ID=" + appointment.getAppointmentID());
        else
            System.out.println("\t\t-Error creating new appointment :(");

        /* Test deleteAppointment()  */
        if (db.deleteAppointment(appointment) == true)
            System.out.println("\t\t-Successfully deleted the new appointment with ID=" + appointment.getAppointmentID());
        else
            System.out.println("\t\t-Error deleting appointment with ID=" + appointment.getAppointmentID());
    }


    /**
     * testHomecareWorker tests the functionality of the HomecareWorker class
     *
     * Author: Vy Bui 
     */
    private static void testHomecareWorker()
    {
      /* Test getWorkerByMatch() */
      HomecareWorker[] workers = HomecareWorker.getWorkerByMatch("Carl", "Carlson");
      if (workers == null)
      {
        System.out.println("\t\t-Error getting getWorkerByMatch(\"Carl\", \"Carlson\")");
        wrap();
        return;
      }
      System.out.println("\t\t+Retrieved " + workers.length + " homercare workers for HomecareWorker " + 
          "getWorkerByMatch(\"Carl\", \"Carlson\")");
      for (int w = 0; w < workers.length; w++)
      {
        HomecareWorker temp = workers[w];
        System.out.println("\t\t\t-" + temp.getWorkerId() + ":" + temp.getWorkerName());
      }
      Timestamp start = null;
      Timestamp end = null;

      Schedule workerSchedule = workers[0].getSchedule(start, end);
      Appointment[] allAppointments = workerSchedule.getAppointments();
      Timeslot[]  allTimeSlots = workerSchedule.getTimeslots();
      	
      if (allAppointments == null || allAppointments.length == 0)
      {
          System.out.println("\t\t-There is no appointment ");
      } 
      else
      {
      System.out.println("\t\t+Retrieved " + allAppointments.length + " appointments for HomecareWorker " +
          "getSchedule()");
          for (int j = 0; j < allAppointments.length; j++)
          {
              Appointment appt = allAppointments[j];
              System.out.println("\t\t\t-" + appt.getStartTime() + " : " +appt.getEndTime());
          }  

      }
    }
    private static void wrap()
    {
        if (db.closeDB() == true)
        {
            System.out.println("\t\t-DB Closed. Bye");
            return;
        }
        else
            System.out.println("\t\t-DB Close Error. Bye anyway!");
    }    
}
